package IOTWeb.com.handler;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import IOTWeb.com.IOTMain;
import IOTWeb.com.agreementclient.WebServer;
import IOTWeb.com.utils.FileOption;
import IOTWeb.com.utils.Utils;

public class SuccessHandler implements HttpHandler {
	@Override
	public void handle(HttpExchange exchange) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Utils utils = new Utils();
					FileOption fileOption = new FileOption();
					//��ȡurl
					System.out.println("url:" + exchange.getRequestURI().toString());
					// ��ò�ѯ�ַ���(get)
					String queryString = exchange.getRequestURI().getQuery();
					Map<String, String> queryStringInfo = utils.formData2Dic(queryString);
					DataInputStream di = new DataInputStream(exchange.getRequestBody());
					String k = new String(di.readLine().getBytes("iso-8859-1"),"gbk");
					String ok = k+"--";
					int count = ok.getBytes("iso-8859-1").length;
					System.out.println(k.getBytes("iso-8859-1").length);
					System.out.println(count);
					String str = new String(di.readLine().getBytes("iso-8859-1"),"gbk");
					System.out.println(str);
					String subs = str.substring(str.lastIndexOf("=")+2,str.length()-1);
					String sub = new String(subs.getBytes("gbk"),"gbk");
					System.out.println(di.readLine());
					System.out.println(di.readLine());
					System.out.println(sub);
				
					// ��ñ��ύ����(post)
					byte bytes[] = new byte[exchange.getRequestBody().available()];
					int len = bytes.length;
					byte[] bs = new byte[len-count-4];
			        System.arraycopy(bytes, 0, bs, 0, len-count-4);
					File file = new File(".",sub);
					FileOutputStream dos=new FileOutputStream(file);
		            dos.write(bs);
					//��ȡresponseͷ
					Headers responseHeaders = exchange.getResponseHeaders();
					//��ȡrequestͷ
					Headers requestHeaders = exchange.getRequestHeaders();
					utils.setResponse(responseHeaders);
					OutputStream os = exchange.getResponseBody();
					PrintStream out = new PrintStream(os);
					utils.getIpLocation(exchange);
					if(exchange.getRequestURI().toString().substring(0,13).equals("/success.html")) {
                    	System.out.println("The user asked for resource: " + "success.html");
                    	fileOption.sendFile(out, "success.html",exchange);
                        out.flush();
                    }
					out.close();
					os.close();
				} catch (IOException ie) {
					ie.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}
